package com.xyzq.cgate.common.response;
/**
 * @Description:
 * @author chenchen
 * @date 2019/6/6 15:44
 */

public class ResponseEntity<T> {

    private Meta meta;
    private T data;

    public ResponseEntity<T> success(T data) {
        this.meta = new Meta(true, 0);
        this.data = data;
        return this;
    }

    public ResponseEntity<T> success() {
        this.meta = new Meta(true, 0);
        return this;
    }

    public ResponseEntity<T> failure(int code, String message) {
        this.meta = new Meta(false, code);
        this.meta.errorMsg = message;
        return this;
    }

    public ResponseEntity<T> failure(String message) {
        this.meta = new Meta(false, 500);
        this.meta.errorMsg = message;
        return this;
    }

    public ResponseEntity<T> setData(T data) {
        this.data = data;
        return this;
    }

    public Meta getMeta() {
        return meta;
    }

    public Object getData() {
        return data;
    }

    public void setMeta(Meta meta) {
        this.meta = meta;
    }

    public class Meta {
        private int code;
        private boolean success;
        private String errorMsg;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public Meta(boolean success, int code) {
            this.setSuccess(success);
            this.setCode(code);
        }
        public boolean isSuccess() {
            return success;
        }

        public void setSuccess(boolean success) {
            this.success = success;
        }

        public String getErrorMsg() {
            return errorMsg;
        }

        public void setErrorMsg(String errorMsg) {
            this.errorMsg = errorMsg;
        }
    }
}
